/*
 * Copyright (C) 2012 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef NETPOWER_H
#define NETPOWER_H

#include <QObject>
#include <libusb-1.0/libusb.h>

static const int VENDOR_ID = 0x8d88;
static const int PRODUCT_ID = 0x0016;
static const int INTERFACE_NUMBER = 0;
static const int TIMEOUT_MS = 5000;

enum device_states
{
    IDLE_STATE = 0,
    ACC_ON_DELAY_STATE,
    PC_ON_STATE,
    PC_SHUTDOWN_STATE,
    PC_ON_DELAY_STATE,
    OS_LOAD_TIME_STATE,
    OS_SHUTDOWN_TIME_STATE,
    ACC_OFF_DELAY_STATE,
    AKB_OFF_DELAY_STATE,
    CHECK_PC_ON_DELAY_STATE,
    R_OUT_DELAY_STATE,
    SLEEP_STATE
};

enum data_bytes
{
    ACC_ON_DELAY = 0,
    OS_LOAD_TIME,
    OS_SHUTDOWN_TIME,
    AKB_MIN,
    PC_ON_DELAY,
    CHECK_PC_ON,
    ACC_OFF_DELAY,
    AKB_OFF_DELAY,
    ACC_OFF_DELAY_H,
    ON_AKB_MIN,
    DONT_USE_ACC,
    R_OUT_DELAY,
    SLEEP_MODE,
    AKB_VOLTAGE_H,
    AKB_VOLTAGE,
    DEVICE_STATUS,
    OUT_VOLTAGE_H,
    OUT_VOLTAGE
};

class netpower : public QObject
{
    Q_OBJECT
public:

    explicit netpower(QObject *parent = 0);
    ~netpower();
    bool init();
    bool deviceOpen;
    int firmvareVersion;
    void readData();
    void writeData();
    unsigned char netpowerData[OUT_VOLTAGE];
    QString deviceStatus;

signals:

public slots:

private:
    struct libusb_device_handle *devh;    
    void readWriteData(unsigned char command);
    bool firstRead;
};

#endif // NETPOWER_H
